home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / dfutil2.zip / TABIT.ZIP / TABIT.BAS next >
BASIC Source File  |  1990-11-10  |  4KB  |  170 lines

  1. 'TABIT replaces spaces with tabs and vice versa
  2. '
  3. ' $INCLUDE: 'qb.bi'
  4.  
  5. DECLARE FUNCTION exists (filename$)
  6.  
  7. DIM SHARED inregs AS RegTypeX, outregs AS RegTypeX
  8. CONST YES = 1, NO = 0
  9.  
  10.       com$ = COMMAND$
  11.       length = LEN(com$)
  12.       max = (length / 2) + 1
  13.       DIM arg$(max)
  14.       true = -1: false = 0: i = 1: num = 0: inword = true
  15.       WHILE i <= length
  16.              ch$ = MID$(com$, i, 1)
  17.              IF ch$ <> " " THEN
  18.                     IF NOT inword THEN inword = true
  19.                     arg$(num) = arg$(num) + ch$
  20.              ELSEIF inword THEN
  21.                     num = num + 1
  22.                     inword = false
  23.              END IF
  24.              i = i + 1
  25.       WEND
  26.       y = 1
  27.       IF NOT arg$(0) = "" THEN GOTO BEGINNING
  28. HELP:
  29.       PRINT " "
  30.       PRINT "TABIT replaces spaces with TABs in a file and vice versa. "
  31.       PRINT "(c)1990 David A. Wesson"
  32.       PRINT " "
  33.       PRINT "Syntax: TABIT  [d:]filename  T or S  num"
  34.       PRINT " where  filename = original file  [drive optional]      "
  35.       PRINT "         T or S  = T  for make TABs  or  S  for make SPACEs"
  36.       PRINT "            num  = number of SPACEs for each TAB "
  37.       PRINT ""
  38.       PRINT "NOTE: TABIT makes a backup of the original file named filename.OLD ."
  39.       END
  40. BEGINNING:
  41.       infile$ = arg$(0)
  42.       IF exists(infile$) = NO THEN GOTO NOFIND
  43.       OPEN infile$ FOR INPUT AS #1
  44.       outfile$ = "temp"
  45.       OPEN outfile$ FOR OUTPUT AS #2
  46.       GOSUB filename
  47.       oldfile$ = file$ + ".OLD"
  48.       switch$ = arg$(1)
  49.       IF switch$ = "" THEN GOTO MISSWITCH
  50.       num = VAL(arg$(2))
  51.       IF num = 0 THEN GOTO MISSNUM
  52.       IF switch$ = "T" OR switch$ = "t" THEN
  53.              GOTO TABIN
  54.       ELSEIF switch$ = "S" OR switch$ = "s" THEN GOTO TABOUT
  55.       ELSE GOTO MISSWITCH
  56.       END IF
  57. TABIN:
  58.       COLOR 15: PRINT "TABIT "; : COLOR 7: PRINT "Fast TAB inserter"
  59. INNUM:
  60.       PRINT "Replacing"; num; "spaces with TABs in "; infile$; ", creating "; oldfile$
  61.       PRINT "Hit [Ctrl]+[Break] to terminate."
  62.       PRINT "Starting time: "; TIME$
  63.       PRINT "   Processing: ";
  64.       z = 0
  65. INCYCLE:
  66.       IF EOF(1) THEN GOTO FINISH
  67.       LINE INPUT #1, l$
  68.       z = z + 1
  69.       strt = 1
  70.       LOCATE , 15: PRINT z;
  71. INSEARCH:
  72.       lfpos = INSTR(strt, l$, STRING$(num, 32))
  73.       IF lfpos < 1 THEN GOTO INDUMP
  74.       GOTO INSPLIT
  75. INNEXTLOOK:
  76.      strt = lfpos + 1: GOTO INSEARCH
  77. INSPLIT:
  78.      lpart$ = LEFT$(l$, lfpos - 1)
  79.      rpos = lfpos + num
  80.      rpart$ = RIGHT$(l$, (LEN(l$) - rpos) + 1)
  81.      s$ = lpart$ + CHR$(9) + rpart$
  82.      l$ = s$
  83.      GOTO INNEXTLOOK
  84. INNEWOUT:
  85.       PRINT #2, s$
  86.       GOTO INCYCLE
  87. INDUMP:
  88.       PRINT #2, l$
  89.       GOTO INCYCLE
  90. '*****************************************************************************
  91. TABOUT:
  92.       COLOR 15: PRINT "TABIT "; : COLOR 7: PRINT "Fast TAB replacer"
  93. OUTNUM:
  94.       PRINT "Replacing TABs with"; num; "spaces in "; infile$; ", creating "; oldfile$
  95.       PRINT "Hit [Ctrl]+[Break] to terminate."
  96.       PRINT "Starting time: "; TIME$
  97.       PRINT "   Processing: ";
  98.       z = 0
  99. OUTCYCLE:
  100.       IF EOF(1) THEN GOTO FINISH
  101.       LINE INPUT #1, l$
  102.       z = z + 1
  103.       LOCATE , 15: PRINT z;
  104.       strt = 1
  105. OUTSEARCH:
  106.       lfpos = INSTR(strt, l$, CHR$(9))
  107.       IF lfpos < 1 THEN GOTO OUTDUMP
  108.       GOTO OUTSPLIT
  109. OUTNEXTLOOK:
  110.       strt = lfpos + num: GOTO OUTSEARCH
  111. OUTSPLIT:
  112.       lpart$ = LEFT$(l$, lfpos - 1)
  113.       rpos = lfpos
  114.       rpart$ = RIGHT$(l$, LEN(l$) - rpos)
  115.       s$ = lpart$ + STRING$(num, 32) + rpart$
  116.       l$ = s$
  117.       GOTO OUTNEXTLOOK
  118. OUTNEWOUT:
  119.       PRINT #2, s$
  120.       GOTO OUTCYCLE
  121. OUTDUMP:
  122.       PRINT #2, l$
  123.       GOTO OUTCYCLE
  124. NOFIND:
  125.       PRINT "ERROR: No file by that name found."
  126.       GOTO HELP
  127. MISSWITCH:
  128.       PRINT "ERROR: Missing T or S switch"
  129.       GOTO HELP
  130. MISSNUM:
  131.       PRINT "ERROR: Missing SPACE NUMBER specifier."
  132.       GOTO HELP
  133. FINISH:
  134.       CLOSE
  135.       IF exists(oldfile$) = YES THEN KILL oldfile$
  136. NOOLD:
  137.       NAME infile$ AS oldfile$
  138.       NAME outfile$ AS infile$
  139.       PRINT ""
  140.       PRINT "  Finish time: "; TIME$
  141.       END
  142. filename:                                         'splits infile$ into
  143.           period = INSTR(infile$, ".")              'file$ and ext$
  144.           IF period = 0 THEN
  145.                     file$ = UCASE$(infile$)
  146.                     ext$ = ""
  147.                     ELSE
  148.                           file$ = UCASE$(LEFT$(infile$, period - 1))
  149.  
  150.                           ext$ = UCASE$(MID$(infile$, period + 1))
  151.           END IF
  152.           RETURN
  153.  
  154. FUNCTION exists (search$)
  155.      savefile$ = search$
  156.      inregs.ax = &H4E00
  157.      inregs.cx = 1     '3 for hidden
  158.      search$ = search$ + CHR$(0)
  159.      inregs.dx = SADD(search$)
  160.      inregs.ds = -1
  161.      CALL INTERRUPTX(&H21, inregs, outregs)
  162.      IF (outregs.flags AND 1) = 1 THEN
  163.             exists = NO
  164.      ELSE
  165.             exists = YES
  166.      END IF
  167.      search$ = savefile$
  168. END FUNCTION
  169.  
  170.